﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class SearchMovies : System.Web.UI.Page
{
    Customer cs;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Customer"] != null)
        {
            cs = (Customer)Session["Customer"];
            string newCust = Request.Params["signup"];

            if (newCust != null && newCust.Equals("true"))
            {
                // this.lblWelcomeText.Visible = true;
            }
            
        }
        else
        {
            Response.Redirect("home.aspx?login=false");

        }

        lblmsg.Text = "";
        lblmsg2.Text = "";

        Movies mv1 = new Movies();
        gvNewListings.DataSource = mv1.getNewMovies();
        gvNewListings.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            lblmsg.Text = "";
            lblmsg2.Text = "";
            Movies mv = new Movies();
            gvSearch.DataSource = mv.getMovies(txtEmail.Text.ToString());
            gvSearch.DataBind();
         
        }
        catch (Exception Ex)
        {
            //  lblmsg.Text = "Save Unsuccessful";
        }

    }

    public void gvSearch_RowCommand(Object sender, GridViewCommandEventArgs e)
    {

        // If multiple ButtonField column fields are used, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "New")
        {
            lblmsg.Text = "";           
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Get the last name of the selected author from the appropriate
            // cell in the GridView control.
           GridViewRow selectedRow = gvSearch.Rows[index];
          
            int index2 = Convert.ToInt32(e.CommandArgument);          
            HiddenField myHiddenField = (HiddenField)gvSearch.Rows[index2].Cells[0].FindControl("hdnMovieID");

            Save(Convert.ToInt32(myHiddenField.Value), "Search");
        }

    }

    public void gvNewListings_RowCommand(Object sender, GridViewCommandEventArgs e)
    {

        // If multiple ButtonField column fields are used, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "New")
        {
            
            lblmsg2.Text = "";
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Get the last name of the selected author from the appropriate
            // cell in the GridView control.
           // GridViewRow selectedRow = gvNewListings.Rows[index];

            int index2 = Convert.ToInt32(e.CommandArgument);
            HiddenField myHiddenField2 = (HiddenField)gvNewListings.Rows[index2].Cells[0].FindControl("hdnMovieID2");

            Save(Convert.ToInt32(myHiddenField2.Value), "NewListings");
        }

    }

    void Save(int movieID, string gridflag)
    {
        //this sub saves data into database
        try
        {
            Movies mv = new Movies();
            int save = mv.saveQueue(cs.Email, movieID);
            if (save == -1)
            {
                if(gridflag =="Search")
                {
                  lblmsg.Text = "Currently, this movie is queued/rented out to you";
                }else{
                  lblmsg2.Text = "Currently, this movie is queued/rented out to you";
                }                
            }
            else
            {
                //lblmsg.Text = "Movie added to cart";
                if (gridflag == "Search")
                {
                    lblmsg.Text = "Movie added to cart";
                }
                else
                {
                    lblmsg2.Text = "Movie added to cart";
                }
            }

        }
        catch (Exception Ex)
        {
            // lblmsg.Text = "Save Unsuccessful";
        }
     }
    public void gvSearch_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {
        gvSearch.PageIndex = e.NewPageIndex;
        Movies mv1 = new Movies();
        gvSearch.DataSource = mv1.getMovies(txtEmail.Text.ToString());
        gvSearch.DataBind();
    }

   

}
